//用状态机实现：字符串“abcabx”的解析

var alreadyFoundC = false
var alreadyFound = true

function match(string){
    alreadyFoundC = false
    alreadyFound = true


    let state = start;
    for(let c of string){
        state = state(c);
    }
    return state === end;
}

function start(c){
    if(c === "a")
        return foundA;
    else
        return start;
}

function end(c) {
    return end;
}

function foundA(c) {
    if(c ==="b")
        return foundB;
    else
        return start(c);
}


function foundB(c) {
    if(c ==="c"){
        if(alreadyFoundC){
            alreadyFoundC = false;
            return start(c);
        }else{
            alreadyFoundC = true
            return foundC;
        }
    }else if(c ==="x"){
        if(alreadyFoundC){
            return end;
        }else{
            return start(c);
        }
    }else
        return start(c);
}



function foundC(c) {
    if(c ==="a")
        return found2A;
    else
        return start(c);
}


function found2A(c) {
    if(c ==="b")
        return found2B;
    else
        return start(c);
}

function found2B(c) {
    if(c ==="x")
        return end;
    else
        return start(c);
}







console.log(match("abcabx"));
console.log(match("abcabxa"));